﻿using System.Collections;
using System.Collections.Generic;

namespace BaseSyntax.Values.StringTest.Yaml.Parsers
{
	public class RectTransformParser:IParser
	{
		public Dictionary<string, object> Parse(
			Dictionary<string, object> dUnity, 
			string fileId, 
			Dictionary<string, object> parent,
			Dictionary<string, object> dCorona
			)
		{
			Dictionary<string, object> d1 = new Dictionary<string, object>();
			
			
			var dSingleUity = dUnity[fileId] as Dictionary<string, object>;

			if (dSingleUity != null)
			{
				//postion
				var dPos = dSingleUity["m_LocalPosition"] as Dictionary<string, object>;
				// todo y轴翻转问题
				if (dPos != null)
				{
					parent["x"] = float.Parse((string) dPos["x"]);
					parent["y"] = -float.Parse((string) dPos["y"]);
				}
				
				//scale
				var dScale = dSingleUity["m_LocalScale"] as Dictionary<string, object>;
				if (dScale != null)
				{
					parent["scaleX"] = float.Parse((string) dScale["x"]);
					parent["scaleY"] = -float.Parse((string) dScale["y"]);
				}
				
				//rotation
				var dRotation = dSingleUity["m_LocalRotation"] as Dictionary<string, object>;
				if (dRotation != null)
				{
					parent["rotation"] = float.Parse((string) dRotation["z"]);
				}
				
				// anchor pivot
				
				// children
				var dChildren = dSingleUity["m_Children"] as ArrayList;
				
				if (dChildren != null && dChildren.Count > 0)
				{
					var dcs = parent["children"] as ArrayList;
				
					for (int i = 0; i < dChildren.Count; i++)
					{
						var transformInfo = dChildren[i] as Dictionary<string, object>;

						if (transformInfo != null)
						{
							string transformId = (string)transformInfo["fileID"];

							var rectTransform = dUnity[transformId] as Dictionary<string, object>;
						
							// 通过rectTransform找到gameObject
							if (rectTransform != null)
							{ 
								var objInfo = rectTransform["m_GameObject"] as Dictionary<string, object>;
								string objId = (string) objInfo["fileID"];
								dcs.Add(objId);
								ParserEntrance.Parse(objId, parent); 
							}
						}
					}
				}
			}

			return d1;
		}
	}
}